package ldz.Show;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import ldz.Manager.Manager;
import ldz.Manager.ManagerDao;
import ldz.Manager.ManagerDaoImpl;
import ldz.lian.DBUtil;
import chj.*;
import xxh.BackgroundPanel;

public class AdminLoginWindow extends JFrame {
    private JTextField adminIdField;
    private JPasswordField passwordField;
    private JButton loginButton;
    private JButton backButton;

    public AdminLoginWindow() {
        // 设置窗口标题
        setTitle("管理员登录");
        // 设置窗口大小
        setSize(400, 300);
        // 设置窗口关闭操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 初始化组件并添加标题说明面板
        initComponentsWithTitle();

        // 显示窗口
        setVisible(true);
    }

    private void initComponentsWithTitle() {
        JPanel mainPanel = new JPanel(new BorderLayout());

        // 创建标题说明面板，使用FlowLayout布局并设置合适的间距
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
        JLabel titleLabel = new JLabel("欢迎使用图书管理系统，请输入管理员账号和密码进行登录");
        titleLabel.setFont(new Font("宋体", Font.PLAIN, 14));
        titlePanel.add(titleLabel);
        mainPanel.add(titlePanel, BorderLayout.NORTH);

        // 创建用于放置返回按钮的面板，采用FlowLayout布局并左对齐
        JPanel northPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        backButton = new JButton("返回");
        backButton.addActionListener(new BackButtonListener());
        northPanel.add(backButton);
        mainPanel.add(northPanel, BorderLayout.WEST);

        BackgroundPanel inputPanel = new BackgroundPanel("src/xxh/img/bk.jpg",300,150);
        inputPanel.setLayout(new GridBagLayout());
        //JPanel inputPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        gbc.gridx = 0;
        gbc.gridy = 0;
        inputPanel.add(new JLabel("管理员编号："), gbc);
        gbc.gridx = 1;
        adminIdField = new JTextField(15);
        inputPanel.add(adminIdField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        inputPanel.add(new JLabel("密码："), gbc);
        gbc.gridx = 1;
        passwordField = new JPasswordField(15);
        inputPanel.add(passwordField, gbc);

        mainPanel.add(inputPanel, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

        loginButton = new JButton("登录");
        loginButton.addActionListener(new LoginButtonListener());
        buttonPanel.add(loginButton);

        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(mainPanel);
    }

    private class LoginButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String adminId = adminIdField.getText();
            char[] passwordChars = passwordField.getPassword();
            String password = new String(passwordChars);

            try {
                // 使用 ManagerDaoImpl 获取管理员数据
                ManagerDao managerDao = new ManagerDaoImpl();
                Connection connection = DBUtil.getConnection();

                String sql = "SELECT * FROM manager WHERE Mno =? AND Mpassword =?";
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, adminId);
                preparedStatement.setString(2, password);

                ResultSet resultSet = preparedStatement.executeQuery();

                if (resultSet.next()) {
                    JOptionPane.showMessageDialog(AdminLoginWindow.this, "登录成功！");

                    LogOn logOn = new LogOn();
                    try {
                        logOn.Chjmain();
                    } catch (UnsupportedLookAndFeelException ex) {
                        throw new RuntimeException(ex);
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    } catch (InstantiationException ex) {
                        throw new RuntimeException(ex);
                    } catch (IllegalAccessException ex) {
                        throw new RuntimeException(ex);
                    }
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(AdminLoginWindow.this, "管理员编号或密码错误，请重新输入。");
                }

                resultSet.close();
                preparedStatement.close();
                connection.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(AdminLoginWindow.this, "数据库连接错误，请检查！");
            }
        }
    }

    private class BackButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // 关闭当前管理员登录窗口
            dispose();
            // 打开登录界面

            LoginWindow loginWindow =  new LoginWindow();
            loginWindow.setLocationRelativeTo(null);
            loginWindow.setVisible(true);
        }
    }
}